//
//  String+Substring.swift
//  TTCOM
//
//  Created by Toj on 1/18/24.
//

import Foundation

// string 截取字符串
public extension String {
    
    var cm_removeSuffixZero: String {
        return cm_cleanZero
    }

    var cm_cleanZero: String {
        guard !isEmpty else { return "" }
        
        var outNumber = self
        if !outNumber.contains(".") { return outNumber }
        
        var i = 1
        while i < count {
            if !outNumber.hasSuffix("0") {
                break
            }
            outNumber.remove(at: outNumber.index(before: outNumber.endIndex))
            i = i + 1
        }
        
        if outNumber.hasSuffix(".") {
            outNumber.remove(at: outNumber.index(before: outNumber.endIndex))
        }
        
        return outNumber
    }
    
    mutating func st_removeLast(string: String) {
        self = st_removingLast(string: string)
    }
    
    func st_removingLast(string: String) -> String {
        var str = self
        while str.hasSuffix(string){
            let startIndex = str.startIndex
            let middleIndex = str.index(startIndex, offsetBy: str.count - string.count)
            let range = middleIndex..<str.endIndex
            str.removeSubrange(range)
        }
        
        return str
    }
    
    var cm_firstCharString: String {
        guard let character = self.first else { return "" }
        return String(character)
    }
    
    var cm_lastCharString: String {
        guard let character = self.last else { return "" }
        return String(character)
    }
    
    /**
     * 从头开始截取, 截取到 to
     */
    func cm_subsring(to tIndex: Int) -> String {
        var _tIndex = tIndex
        guard _tIndex > 0 else { return "" }
        if _tIndex > count { _tIndex = count }
        
        let index = String.Index(utf16Offset: _tIndex, in: self)
        let subString = self[..<index]
        let string = String(subString)
        return string
    }
    
    /**
     * 从from开始截取到末尾
     */
    func cm_subsring(from fIndex: Int) -> String {
        guard fIndex >= 0, fIndex < count else { return "" }
        let index = String.Index(utf16Offset: fIndex, in: self)
        let subString = self[index...]
        let string = String(subString)
        return string
    }
    
    /**
     * 从from位开始截取到末尾
     */
    func st_subsring(from fIndex: Int) -> String {
        guard fIndex >= 0, fIndex < count else { return "" }
        let index = String.Index(utf16Offset: fIndex, in: self)
        let subString = self[index...]
        let string = String(subString)
        return string
    }
    
    /**
     * 从头开始截取, 截取到 to
     */
    func st_subsring(to string: String) -> String {
        guard let index = firstIndex(of: Character(string)) else { return "" }
        let subString = self[..<index]
        let string = String(subString)
        return string
    }
    
    /**
     * 从string后面开始截取到末尾
     */
    func st_subsring(from string: String, reference isContains: Bool = false) -> String {
        guard var fIndex = firstIndex(of: Character(string)) else { return "" }
        
        if !isContains { fIndex = index(fIndex, offsetBy: 1) }
        
        let subString = self[fIndex...]
        let string = String(subString)
        return string
    }
}
